﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BLL;
using Utility;
using System.Collections.Generic;
using Model;

public partial class Vehicle_Base_VehicleMgr : System.Web.UI.Page
{
    
    private void FuncCheck()
    {
        string pages = "F2_1&F2_1_1&F2_1_2&F2_1_3";
        Hashtable fc = RightChecker.GetPageFuncHT(pages.Split('&'), (IList<Function>)Session["FcList"]);

        btn_Add.Visible = (bool)fc["F2_1_1"];
        btn_Import.Visible = (bool)fc["F2_1_1"];
        btn_Delete.Visible = (bool)fc["F2_1_3"];
;

        if (!(bool)fc["F2_1_2"])
        {
            GridView1.Columns[9].Visible = false;
        }
    }

    VehicleBLL vBLL = new VehicleBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FuncCheck();

            string strWhere = "";
            if (Session["GroupList"] != null)
            {
                strWhere += " OR  ObjectGroup IN(" + Session["GroupList"].ToString() + ")";
            }
            if (Session["SubOrganList"] != null)
            {
                strWhere += " OR  OrganID IN(" + Session["SubOrganList"].ToString() + ")";
            }
            if (strWhere.Length > 0)
            {
                strWhere = strWhere.Substring(4);
                Session["Where"] = strWhere;
                BindGridView();
            }
            else
            {
                UtilityService.Alert(this.Page, "请选设定数据组!");
            }

        }
    }

    ////绑定分页和GridView方法
    private void BindGridView()
    {
        DataSet ds = new VehicleBLL().GetList(Session["Where"].ToString());
        //初始化分页数据源实例
        PagedDataSource pds = new PagedDataSource();
        //设置总行数
        AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
        //设置分页的数据源
        pds.DataSource = ds.Tables[0].DefaultView;
        //设置当前页
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        //设置每页显示页数
        pds.PageSize = AspNetPager1.PageSize;
        //启用分页
        pds.AllowPaging = true;
        //设置GridView的数据源为分页数据源
        GridView1.DataSource = pds;
        //绑定GridView
        GridView1.DataBind();
    }

    protected void btn_Add_Click(object sender, EventArgs e)
    {

    }
    protected void btn_Modity_Click(object sender, EventArgs e)
    {

    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        string idList = string.Empty;
        foreach (GridViewRow dr in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)dr.FindControl("chk");
            if (chk.Checked)
            {
                string _id = "'" + dr.Cells[1].Text.Trim() + "'";
                idList += _id + ",";
            }

        }
        if (idList.Length > 0)
        {
            idList = idList.TrimEnd(',');
            bool re = new VehicleBLL().DeleteList(idList);
            if (re)
            {
                UtilityService.AlertAndRedirect(this.Page, "删除成功!", "VehicleMgr.aspx");
            }
            else
            {
                UtilityService.Alert(this.Page, "删除失败!");
            }
        }
    }
    protected void chk_All_CheckedChanged1(object sender, EventArgs e)
    {

    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string strWhere = "";
        if (Session["GroupList"] != null)
        {
            strWhere += " OR  ObjectGroup IN(" + Session["GroupList"].ToString() + ")";
        }
        if (Session["SubOrganList"] != null)
        {
            strWhere += " OR  OrganID IN(" + Session["SubOrganList"].ToString() + ")";
        }
        if (strWhere.Length > 0)
        {
            strWhere = "(" + strWhere.Trim().Substring(4) + ")";
            strWhere += " AND (VehicleID LIKE '%" + txt_Search.Text.Trim() + "%')";

            Session["Where"] = strWhere;
            BindGridView();
        }
        else
        {
            UtilityService.Alert(this.Page, "请选设定数据组!");
            return;
        }

    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView();
    }


}
